package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.HomeVideo;
import com.xindaoapp.happypet.bean.HomeVideo.Video;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;

public class HomeVideoAdapter extends BaseAdapter {
    private final List<HomeVideo.Video> videos;
    private final Context context;

    public HomeVideoAdapter(List<Video> videos, Context context) {
        super();
        this.videos = videos;
        this.context = context;
    }

    @Override
    public int getCount() {
        return videos.size();
    }

    @Override
    public Object getItem(int position) {
        return videos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItems(List<Video> video) {
        videos.addAll(video);
        notifyDataSetChanged();
    }

    public void clearItems() {
        if (null != videos) {
            videos.clear();
        }
    }

    public List<HomeVideo.Video> getItems() {
        if (null != videos) {
            return videos;
        } else {
            return null;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.home_video_item, null);
            holder = new ViewHolder();
            holder.video_pic = (ImageView) convertView.findViewById(R.id.video_pic);
            holder.video_title = (TextView) convertView.findViewById(R.id.video_title);
            holder.look_time = (TextView) convertView.findViewById(R.id.look_time);
            holder.care_time = (TextView) convertView.findViewById(R.id.care_time);
            holder.message_time = (TextView) convertView.findViewById(R.id.message_time);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.video_pic.setScaleType(ScaleType.CENTER_CROP);
        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(videos.get(position).videocover,
                    holder.video_pic);
        } else {
            holder.video_pic.setImageResource(R.drawable.picture_loading);
        }
        holder.video_title.setText(videos.get(position).subject);
        holder.look_time.setText(videos.get(position).views);
        holder.care_time.setText(videos.get(position).recommend_add);
        holder.message_time.setText(videos.get(position).replies);
        // holder.time.setText(videos.get(position).subject);

        String time = CommonUtil.formatDate();
        String year = time.substring(0, 4);
        String hour = time.split(" ")[1].split(":")[0];
        String min = time.split(" ")[1].split(":")[1];

        String mTime = videos.get(position).addtime;
        String mYear = mTime.substring(0, 4);
        String mMonth = mTime.split(" ")[0].split("-")[1];
        String mDay = mTime.split(" ")[0].split("-")[2];
        String mHour = mTime.split(" ")[1].split(":")[0];
        String mMin = mTime.split(" ")[1].split(":")[1];

        if (year.equals(mYear)) {// 说明是今年
            if (videos.get(position).addtime.split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
                holder.time.setText(mHour + ":" + mMin);
            } else {
                holder.time.setText(mMonth + "-" + mDay);
            }
        } else {
            holder.time.setText(mTime.split(" ")[0]);
        }
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, videos.get(position).tid);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView video_pic;
        TextView video_title;
        TextView look_time;
        TextView care_time;
        TextView message_time;
        TextView time;
    }
}
